package net.sharpf2.api.util;

import java.awt.Point;
import net.sharpf2.api.Globals;
import net.sharpf2.api.Methods;

/**
 *
 * @author Rick van Biljouw
 */
public class Mathematics {

    public static final int[] SIN_TABLE = new int[16384];
    public static final int[] COS_TABLE = new int[16384];

    static {
        final double d = 0.00038349519697141029D;
        for (int i = 0; i < 16384; i++) {
            SIN_TABLE[i] = (int) (32768D * Math.sin(i * d));
            COS_TABLE[i] = (int) (32768D * Math.cos(i * d));
        }
    }
    
    /**
     * Checks if a coordinate is within gamescreen bounds
     * @param   screenPosition  Screen Position.
     * @return  True if is on screen, otherwise false.
     */
    public static boolean coordinateOnScreen(Point screenPosition) {
        return screenPosition.x > 4 && screenPosition.x < (762 - 253) && screenPosition.y > 4 && screenPosition.y < (503 - 169);
    }
    
    /**
     * Generates a random number.
     * @param   min Minimum number.
     * @param   max Maximum number.
     * @return  Randomly generated number.
     */
    public static int random(int min, int max) {
        return ((int) (Math.random() * (max - min))) + min;
    }     
    
    /**
     * Returns how much the angle to a specified tile is.
     * @param   p   Tile.
     * @return  Angle.
     */
    public static int tileToAngle(Point p) {
        Point mp = Methods.getMyPlayer().getContent().getWorldPosition();
        int a = (int)(Math.toDegrees(Math.atan2(p.y - mp.y, p.x - mp.x)));
        return a >= 0 ? a : 360+a;
    }
    
    /**
     * Calculate distance between 2 points. Start point is current player's position
     * @param   d Destination point
     * @return  distance
     */
    public static int distanceToPlayer(Point d){
        return (int) d.distance(Globals.CLIENT.getLocalPlayer().getWorldPosition());
    }
    
    public static Point[] reverse(Point[] b) {
       int l  = 0;
       int r = b.length-1;

       while (l < r) {
          Point temp = b[l]; 
          b[l]  = b[r]; 
          b[r] = temp;

          l++;
          r--;
       }
       
       return b;
    }
    
}
